import { request, downloadBlobFile } from 'src/utils';

export interface BuildingDto {
  buildingId: IdType; // 楼宇Id
  communityId: IdType; // 小区ID
  areaId: IdType; // 小区ID
  parentId: IdType; // 上级id
  buildingNo: string; // 楼宇编号
  buildingName: string; // 楼宇名称
  buildingLevel: number; // 类型：楼栋，单元楼
  buildingUsage: number; // 房间用途
  buildingType: number; // 户型
  buildingDirection: number; // 朝向
  floor: number; // 层数
  remark: string; // 备注说明
  createdBy: string; // 创建人
  createdTime: Date; // 创建时间
  children: BuildingDto[];
}

const baseUrl = '/api/community/building';

export function getAreaTree(): Promise<SelectOption[]> {
  return request.get(baseUrl + '/area-tree');
}

export function getBuildingTree(): Promise<BuildingDto[]> {
  return request.get(baseUrl + '/building-tree');
}
export function getMainBuilding(areaId: IdType): Promise<SelectOption[]> {
  return request.get(baseUrl + '/main-building?areaId=' + areaId);
}

/**
 * 查单个
 */
export function readData(buildingId: IdType): Promise<BuildingDto> {
  return request.get(baseUrl + '/' + buildingId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<BuildingDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(building: BuildingDto): Promise<BuildingDto> {
  return building.buildingId ? request.put(baseUrl + '/' + building.buildingId, building) : request.post(baseUrl, building);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<BuildingDto>;
